/*!
 * 面板控件.
 * ecp.qzz.ui.button.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.window"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui");
	/**
	 * 面板控件.
	 */
	qzz.ui.button = qzz.Class(qzz.ui.window, {
		/**
		 * 创建函数
		 */
	    _create:function() {
	    	this.base();
            this.className = "Button";
            this._common = [];
            this._stateValue = 0;
            this.getOption().icon = this.getOption().icon || "";
            this.getOption().style = this.getOption().style || "H";
            if(this.getOption().common != null) {
            	if(typeof this.getOption().common == "string") {
            	    this._common.push(this.getOption().common);
            	} else {
            		this._common = this.getOption().common;
            	}
            } else {
            	this._common = ["button_common"];
            }
	    },
	    /**
	     * 设置图标.
	     */
	    setIcon:function(url) {
	    	this.getOption().icon = "url(\""+url+"\") no-repeat scroll 50% 50% transparent";	    	
	    },
	    /**
	     * 渲染.
	     */
	    _render:function() {	    	
	    	//兼容用Input配置的情况.
	    	if(this.control && this.control[0].tagName === "INPUT") {
	    		var conDiv = $(this.createElement("div"));
	    		conDiv.attr("dataField", this.control.attr("dataField"));
	    		conDiv.attr("data-option", this.control.attr("data-option"));
	    		conDiv.attr("id", this.control.attr("id"));
	    		conDiv.attr("name", this.control.attr("name"));
	    		conDiv.attr("width", this.control.attr("width"));
	    		this.control.replaceWith(conDiv);
	    		this.control = conDiv;
	    		this.control.data("_ecpUI", this);
	    		this.thisWindow = conDiv[0];
	    	}
	    	this.base();
	    	if(this.thisWindow.parentNode != null && this.thisWindow.parentNode.style.position !== "") {
	    	    this.thisWindow.style.position = "absolute";
	    	}
	    	this._cTable = this.createElement("TABLE");	    	
	    	var tr = this._cTable.insertRow(0);
	    	this._tdIcon = tr.insertCell(0);
	    	//this.setStyle(this._tdIcon, "c_common_td");
	    	if(this.getOption().style == "H") {
	    	    this._tdCaption = tr.insertCell(1);
	    	} else {
	    		tr = this._cTable.insertRow(1);
	    		this._tdCaption = tr.insertCell(0);
	    	}
	    	
	    	//this.setStyle(this._tdCaption, "c_common_td");
	    	
	    	if(this.getOption().icon === "") {
	    		this._tdIcon.style.display = "none";
	    	}	
	    	if(this._tdCaption === "") {
	    		this._tdCaption.style.display = "none";
	    	}
	    	this.refresh();
	    },
	    setParent:function(pnode) {
	    	this.base(pnode);
	    	if(this.thisWindow.parentNode != null && this.thisWindow.parentNode.style.position !== "") {
	    	    this.thisWindow.style.position = "absolute";
	    	}
	    },
	    refreash:function() {
	    	this.refresh();
	    },
	    /**
	     * 刷新.
	     */
	    refresh:function(isclick) {
	    	if(this.getOption().style == "H" && this._cTable.rows.length == 2) {
	    		this._cTable.deleteRow(1);
	    		this._tdCaption = this._cTable.rows[0].insertCell(1);
	    	} else if(this.getOption().style == "V" && this._cTable.rows[0].length == 1) {
	    		this._cTable.rows[0].deleteCell(1);
	    		this._tdCaption = this._cTable.insertRow(1).insertCell(0);
	    	}
	    	//图标
	    	if(this.getOption().icon === "" || this.getOption().icon == null) {
	    		if(this.getOption().style == "H") {
	    		    this._tdIcon.style.display = "none";
	    		} else {
	    			this._tdIcon.parentElement.style.display = "none";
	    		}
	    	} else {
	    		this._tdIcon.style.display = "";
	    		this._tdIcon.parentElement.style.display = "";
	    	}
	    	
	    	this.setStyle(this._tdIcon, "button_icon");
	    	//标题
	    	if(this.getOption().caption === "" || this.getOption().caption == null) {
	    		if(this.getOption().style == "H") {
	    		    this._tdCaption.style.display = "none";
	    		} else {
	    			this._tdCaption.parentElement.style.display = "none";
	    		}
	    	} else {
    			this._tdCaption.style.display = "";
    			this._tdCaption.parentElement.style.display = "";
	    	}
	    	this.setStyle(this._tdCaption, "button_caption");
	    	//显示图标
	    	this._tdIcon.style.background = this.getOption().icon;
	    	this._setHtml(this._tdCaption, this.getOption().caption || "", false);
	    	
	    	this.setStyle(this._cTable, "button_table");
	    	this.thisWindow.style.cursor = "pointer";
	    	
	    	this.thisWindow.appendChild(this._cTable);
	    	
	    	if(this.getOption().move === true && isclick === true) {
	    		this.setStyle(this._cTable, "button_table_move");
	    		this.setStyle(this.thisWindow, "button_div_move");
	    		if(this.focus) {
		    		this.addStyle(this.thisWindow, this._common[this._stateValue] + " active focus");
		    	} else {
		    	    this.addStyle(this.thisWindow, this._common[this._stateValue]);
		    	}
	    	} else {
	    		if(this.focus) {
		    		this.setStyle(this.thisWindow, this._common[this._stateValue] + " active focus");
		    	} else {
		    	    this.setStyle(this.thisWindow, this._common[this._stateValue]);
		    	}
	    	}
	    	
	    	if(this.getOption().hint != null) {
	    		this.thisWindow.setAttribute("title", this.getOption().hint);
	    	}
	    	
	    	if(this.getOption().disabled == true) {
	    		this.addStyle(this._cTable, "button_disabled");
	    		this.addStyle(this.thisWindow, "button_disabled");
	    	}
	    },
	    
	    /**
	     * 添加样式内容.
	     */
	    addStyle:function(commonStyle) {
	    	if(arguments.length > 1) {
	    		this.base.apply(this, arguments);
	    	} else {
		    	if(commonStyle != null && commonStyle !== "") {
		    		if(this.getOption().common == null) {
		    			this._common = [];
		    		}
		    		this._common.push(commonStyle);
		    		this.refresh();
		    	}
	    	}
	    },
	    /**
	     * 清空样式.
	     */
	    clearStyle:function() {
            this._common = [];
            this._stateValue = 0;
	    },
	    /**
	     * 设置状态
	     */
	    setState:function(value) {
	    	if(value < this._common.length && value >= 0 && this._stateValue != value) {
	    	    this._stateValue = value;
	    	    this.refresh();
	    	}
	    },
	    /**
	     * 鼠标按下.
	     */
	    doMouseDown:function(ev) {
	    	if(this.getOption().disabled == true) return false;
	    	this.trigger("onmousedown", [ev]);
	    	return false;
	    },
	    /**
	     * 失去焦点状态.
	     */
	    doBlur:function() {
	    	this.base();
	    	if(this.getOption().canFocus === true) {
		    	if(this.focus) {
		    		this.setStyle(this.thisWindow, this._common[this._stateValue] + " active focus");
		    	} else {
		    		this.setStyle(this.thisWindow, this._common[this._stateValue]);
		    	}
	    	}
	    	this.refresh(true);
	    },
	    /**
	     * 鼠标弹起.
	     */
	    doMouseUp:function(ev) {	    	
	    	if(this.getOption().disabled == true || this.hasSelect === false) return false;
	    	var count = this._common.length;
	    	if(count > 0) {
	    		//状态转换
		    	this._stateValue++;
		    	this._stateValue = this._stateValue % count;
		    	if(this.getOption().canFocus === true) {
			    	if(this.focus) {
			    		this.setStyle(this.thisWindow, this._common[this._stateValue] + " active focus");
			    	} else {
			    		this.setStyle(this.thisWindow, this._common[this._stateValue]);
			    	}
		    	}
		    	this.refresh(true);
	    	}
	    	if(this.focus) {
		    	this.trigger("onmouseup", [ev]);
		    	this.trigger("onclick", [ev]);
		    	this.trigger("onClick", [ev]);
	    	}
	    	return false;
	    }
	});
	
	/**
	 * 按钮控件.
	 */
	_bindRenderFunc("qzzbutton", qzz.ui.button);
	return qzz.ui.button;
});
